<?php

namespace App\Service;

use Phalcon\Di;
use App\Models\EwMemberLog;
use App\Models\EwHouse;//房源
use App\Models\EwNews;//资讯
use App\Models\EwDeco;//装修
use App\Models\EwData;//数据
use App\Models\EwVideo;//视频
use App\Models\EwDecoComp;//装修公司

class MemberLogService extends BaseService
{
	const POS_LOGIN = 1;//登陆
	const POS_SEARCH = 2;//搜索关键词
	const POS_REMAIN = 3;//停留页面
	const POS_PAY  = 4;//支付
	const POS_VISIT = 5;//浏览记录
	const GO_URL  = "";

	public function initialize()
	{
		$config = Di::getDefault()['config'];
		$this->GO_URL = $config->gourl;
	}

	public static function getList($params, $count = FALSE)
	{
		$params->member_id = property_exists($params,'member_id') ? $params->member_id : 0;
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['l.list_id AS id,l.table_id,l.json_content,l.create_time,
			IFNULL((SELECT count(c.id) FROM App\Models\EwMemberCollect c WHERE c.list_id = l.list_id AND c.table_id = l.table_id AND c.member_id =  l.member_id  ),0) AS is_collect
			']);
		$builder->addFrom("App\Models\EwMemberLog", 'l');

		//member_id
		if (isset($params->member_id) && $params->member_id)
			$builder->andwhere("l.member_id = ".$params->member_id);

		//table_id
		if (isset($params->table_id) && $params->table_id)
			$builder->andwhere("l.table_id = ".$params->table_id);

		//list_id
		if (isset($params->list_id) && $params->list_id)
			$builder->andwhere("l.list_id = ".$params->list_id);

		//pos
		if (isset($params->pos) && $params->pos)
			$builder->andwhere("l.pos = ".$params->pos);

		//总条数
		if ($count)
			return $builder->getQuery()->execute()->count();

		$sql = " l.create_time DESC";

		//order_by 排序
		if (isset($params->order_by))
			$sql = parent::orderBy($params->order_by);	
		$builder->orderBy($sql);

		//分页
		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();

		return $result->execute();	
	}

	public static function getSearch($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['l.id,l.member_id,m.nickname,l.json_content,l.pos,l.group_id,l.type_id,l.create_time']);
		$builder->addFrom("App\Models\EwMemberLog", 'l');
		$builder->leftJoin("App\Models\EwMember", 'l.member_id = m.id' , 'm');

		//member_id
		if (isset($params->member_id) && $params->member_id)
			$query->andwhere("l.member_id = ".$params->member_id);

		//table_id
		if (isset($params->table_id) && $params->table_id)
			$query->andwhere("l.table_id = ".$params->table_id);

		//list_id
		if (isset($params->list_id) && $params->list_id)
			$query->andwhere("l.list_id = ".$params->list_id);

		//pos
		if (isset($params->pos) && $params->pos)
			$query->andwhere("l.pos = ".$params->pos);

		$sql = " s.id DESC, s.create_time DESC";
		//order_by 排序
		if (isset($params->order_by))
			$sql = parent::orderBy($params->order_by);	
		$query->orderBy($sql);

		if ($count)
			return $builder->getQuery()->execute()->count();

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();
	}

	public static function getInfo($id = 0)
	{
		return EwMemberLog::findFirst($id);
	}

	public static function count()
	{
		return EwMemberLog::count();
	}

	/**
	 * [getJsonInfo 通过table_id 和 list_id 获取json数据]
	 * @param  [type] $table_id   [description]
	 * @param  [type] $list_id [description]
	 * @return [type]      [description]
	 */
	private static function getJsonInfo($table_id ,$list_id)
	{
		return MemberCollectService::getJsonInfo($table_id ,$list_id);
	}

	/**
	 * [getLogInfo 通过member_id table_id 和 list_id 获取浏览记录 判断是否以浏览]
	 * @param  [type] $通过member_id   [description]
	 * @param  [type] $table_id   [description]
	 * @param  [type] $list_id [description]
	 * @return [type]      [description]
	 */
	public static function getLogInfo($member_id, $table_id ,$list_id)
	{
		return EwMemberLog::findFirst(['member_id = ' . $member_id . ' AND table_id = ' . $table_id . ' AND list_id = ' . $list_id]);
	}

	/**
	 * [do 浏览记录]
	 * @param  [type] $data->member_id [登陆会员id]
	 * @param  [type] $data->table_id  [对应的group_id]
	 * @param  [type] $data->list_id   [对应的主键id]
	 * @return [type]                  [description]
	 */
	public static function do($data)
	{
		//判断是否已经浏览过本条信息
		$info = self::getLogInfo($data->member_id, $data->table_id, $data->list_id);

		if ($info){//已经存在浏览记录则更新时间为最新浏览时间
			$info->create_time = date("Y-m-d H:i:s", time());
			return $info->save();
		}else{
			$content = self::getJsonInfo($data->table_id, $data->list_id);

			if($content){//浏览
				$arr = new \stdClass();

				// $arr->go_url = $content["go_url"];
				$arr->member_id = $data->member_id;
				$arr->table_id = $data->table_id;
				$arr->list_id  = $data->list_id;
				$arr->json_content = json_encode($content);
				$arr->pos = self::POS_VISIT;
				
				$model = new EwMemberLog();
				return $model->save((array)$arr);
			}
		}
	}

	/**
	 * [del 浏览记录]
	 * @param  [type] $data->member_id [登陆会员id]
	 * @param  [type] $data->table_id  [对应的group_id]
	 * @param  [type] $data->list_id   [对应的主键id]
	 * @return [type]                  [description]
	 */
	public static function del($data)
	{
		//判断是否已经浏览过本条信息
		$info = self::getLogInfo($data->member_id, $data->table_id, $data->list_id);
		$row->delete();
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwMemberLog::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwMemberLog;
		if (isset($data->id) && $data->id)
			$row = EwMemberLog::findFirst($data->id);

		return $row->save((array)$data);
	}
}